﻿#include <QScrollBar>
#include "CAlarmTable.h"
#include "CGlobal.h"
#include "CAlarmDetailsDlg.h"
#include "CAlarmRecordQuery.h"

CAlarmTable::CAlarmTable(CDevDataCam* pDevData,QWidget *parent)
	: m_pDevData(pDevData),QWidget(parent)
{
	ui.setupUi(this);
	initUI();
	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(slotRefreshUI()));
	connect(ui.pushButtonMore, &QPushButton::clicked, this, &CAlarmTable::slotMore);
	connect(this, &CAlarmTable::sigShowImg, this, &CAlarmTable::slotShowImg);
}

CAlarmTable::~CAlarmTable()
{
	m_pTimer->stop();
	// 断开所有连接，防止信号队列中的调用
	disconnect(m_pTimer, nullptr, this, nullptr);
	delete m_pTimer;
	m_pTimer = nullptr;
}

void CAlarmTable::initUI()
{
	m_pDevData->m_pMutex->lock();
	ui.labelCamerName->setText(m_pDevData->m_sCamerName);
	m_pDevData->m_pMutex->unlock();
	//禁止编辑
	ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	//单选行
	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	//模式为单选
	ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
	//行末尾自动拉伸填充
	ui.tableView->horizontalHeader()->setStretchLastSection(true);
	//左边序号列隐藏
	ui.tableView->verticalHeader()->setVisible(false);
	m_pModel = new QStandardItemModel(this);
	QStringList headerListAction;		//水平表头
	headerListAction << A2T("序号") << A2T("摄像头名") << A2T("报警类型") <<
		A2T("报警时间") << A2T("报警等级") << A2T("是否确认")<< A2T("操作");

	QList<quint16> listColumnWidth;		//列宽
	listColumnWidth << 50 << 180 << 150 << 180 << 120 <<100;

	//设置列数
	m_pModel->setColumnCount(headerListAction.count());
	//设置表头
	m_pModel->setHorizontalHeaderLabels(headerListAction);
	ui.tableView->setModel(m_pModel);
	//设置列宽
	for (int i = 0; i < listColumnWidth.count(); i++)
	{
		ui.tableView->setColumnWidth(i, listColumnWidth.at(i));
	}
}

void CAlarmTable::slotShowImg(QString sGuid)
{
	CAlarmDetailsDlg oper(sGuid);
	oper.exec();
}

void CAlarmTable::slotMore()
{
	CAlarmRecordQuery oper;
	oper.exec();
}

void CAlarmTable::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		m_pTimer->start(1000);
	}
	else
	{
		m_pTimer->stop();
	}
}

void CAlarmTable::slotRefreshUI()
{
	if (!isVisible())
	{
		return;
	}
	// 保存当前的滚动条位置和选中行
	int verticalScrollValue = ui.tableView->verticalScrollBar()->value();
	int horizontalScrollValue = ui.tableView->horizontalScrollBar()->value();
	m_pDevData->m_pMutex->lock();
	QVector<AlarmItem> vecAlarmItem = m_pDevData->m_vecAlarm;
	m_pDevData->m_pMutex->unlock();
	ui.tableView->blockSignals(true);
	m_pModel->removeRows(0, m_pModel->rowCount());
	for (int i = 0; i < vecAlarmItem.size(); i++)
	{
		QList<QStandardItem*> item;
		item.append(new QStandardItem(QString::number(i)));//序号
		item.append(new QStandardItem(vecAlarmItem[i].sCamerName));//摄像头名称
		item.append(new QStandardItem(vecAlarmItem[i].sAlarmType));//报警类型
		item.append(new QStandardItem(vecAlarmItem[i].sAlarmTime));//报警时间
		item.append(new QStandardItem(QString::number(vecAlarmItem[i].nAlarmLevel)));//报警等级
		if (vecAlarmItem[i].nIsConfirm == 1)
		{
			item.append(new QStandardItem(A2T("是")));//是否确认
		}
		else
		{
			item.append(new QStandardItem(A2T("否")));//是否确认
		}
		item.append(new QStandardItem(""));//操作
		//设置居中显示
		for (int nRow = 0; nRow < item.count(); nRow++)
		{
			item.at(nRow)->setTextAlignment(Qt::AlignCenter);
		}
		m_pModel->insertRow(m_pModel->rowCount(), item);
		// 添加按钮到操作列
		QPushButton *btn = new QPushButton(A2T("查看明细"));
		btn->setProperty("row", i);
		btn->setProperty("guid", vecAlarmItem[i].sGuid);
		connect(btn, &QPushButton::clicked, this, [this]() {
			QPushButton *senderBtn = qobject_cast<QPushButton*>(sender());
			if (senderBtn) {
				QString sGuid = senderBtn->property("guid").toString();
				emit sigShowImg(sGuid);
			}
		});
		ui.tableView->setIndexWidget(m_pModel->index(m_pModel->rowCount() - 1, 6), btn);
		if (m_pModel->rowCount() > 50)
		{
			m_pModel->removeRows(m_pModel->rowCount() - 1, 1);//移除最后一行
		}
	}
	// 恢复滚动条位置
	ui.tableView->verticalScrollBar()->setValue(verticalScrollValue);
	ui.tableView->horizontalScrollBar()->setValue(horizontalScrollValue);

	// 解除信号阻塞
	ui.tableView->blockSignals(false);
}
